﻿using System;
using System.Linq;
using Web.Manage.Models;
using SilenthinkContract.Domain;
using System.Collections.Generic;

namespace Web.Manage.Util
{
    public static class ProductHelper
    {
        public static void ProcessAttributes(Product product, string[] attributes, IList<ProductControllerModel.ProductVariantSaleAttribute> saleAttributes)
        {
            if (product == null) return;
            var dateTime = DateTime.Now;
            var productVariants = new List<ProductVariant>();
            var productAttributes = new List<ProductAttribute>();
            if (attributes != null)
            {
                foreach (var attribute in attributes)
                {
                    if (string.IsNullOrEmpty(attribute) || attribute == "请选择") continue;
                    var temp = attribute.Split(':');
                    if (string.IsNullOrEmpty(temp[0]) || string.IsNullOrEmpty(temp[1])) continue;
                    var a = new ProductAttribute
                                {
                                    Id = Guid.NewGuid(),
                                    ProductCategoryCode = product.CategoryCode,
                                    AttributeNameCode = temp[0],
                                    AttributeValueCode = temp[1],
                                    CreatedOn = dateTime
                                };
                    productAttributes.Add(a);
                }
            }
            if (saleAttributes.Any())
            {
                foreach (var attribute in saleAttributes)
                {
                    if (string.IsNullOrEmpty(attribute.Code) || !attribute.Quantity.HasValue || !attribute.Price.HasValue) continue;
                    Guid guid;
                    var variantCode = string.Empty;
                    if (!Guid.TryParse(attribute.Code, out guid))
                    {
                        var temp = attribute.Code.Split('-');
                        if (temp.Length == 1)
                        {
                            var attr = attribute.Code.Split(':');
                            var a = new ProductAttribute
                                        {
                                            Id = Guid.NewGuid(),
                                            ProductCategoryCode = product.CategoryCode,
                                            AttributeNameCode = attr[0],
                                            AttributeValueCode = attr[1],
                                            CreatedOn = dateTime
                                        };
                            variantCode = attr[1];
                            productAttributes.Add(a);
                        }
                        if (temp.Length == 2)
                        {
                            foreach (var s in temp)
                            {
                                var attr = s.Split(':');
                                if (string.IsNullOrEmpty(attr[0]) || string.IsNullOrEmpty(attr[1])) continue;
                                if (
                                    productAttributes.Count(
                                        o => o.AttributeNameCode == attr[0] && o.AttributeValueCode == attr[1]) < 1)
                                {
                                    var a = new ProductAttribute
                                                {
                                                    Id = Guid.NewGuid(),
                                                    ProductCategoryCode = product.CategoryCode,
                                                    AttributeNameCode = attr[0],
                                                    AttributeValueCode = attr[1],
                                                    CreatedOn = dateTime
                                                };
                                    productAttributes.Add(a);
                                }
                                variantCode += attr[1]+",";
                            }
                            variantCode = variantCode.Trim(',');
                        }
                    }
                    var variant = new ProductVariant
                    {
                        Id = Guid.NewGuid(),
                        VariantCode = variantCode,
                        VariantPrice = attribute.Price.Value,
                        VariantQuantity = attribute.Quantity.Value,
                        CreatedBy = product.CreatedBy,
                        CreatedOn = dateTime
                    };
                    productVariants.Add(variant);
                }
            }
            product.ProductVariants = productVariants;
            product.ProductAttributes = productAttributes;
        }
    }
}